package com.martin.dms.dataservices.hib;

import java.util.List;

import org.hibernate.Session;

import com.martin.dms.dataservices.SupplierDataService;
import com.martin.dms.model.Supplier;
import com.martin.dms.model.hib.ItemHib;
import com.martin.dms.model.hib.SupplierHib;

/**
 * Hibernate implementation of the Supplier data service.
 * 
 * @author mtsvetkov.
 */
public class SupplierDataServiceHib implements SupplierDataService {

    private static final String SUPPLIER_GET_ALL_QUERY = "Supplier.getAll";

    private Session session;

    /**
     * Constructor that expects a hibernate session.
     * 
     * @param session - the current hibernate session.
     */
    public SupplierDataServiceHib(Session session) {
        this.session = session;
    }

    /**
     * {@inheritDoc}
     */
    public Supplier create(Supplier dmsObject) {
        SupplierHib supplier = new SupplierHib();
        supplier.setAddress(dmsObject.getAddress());
        supplier.setEmail(dmsObject.getEmail());
        supplier.setName(dmsObject.getName());
        supplier.setPhoneNumber(dmsObject.getPhoneNumber());
        session.save(supplier);
        return supplier;
    }

    /**
     * {@inheritDoc}
     */
    public void delete(long id) {
        SupplierHib supplier = get(id);
        session.delete(supplier);
    }

    /**
     * {@inheritDoc}
     */
    public SupplierHib get(long id) {
        return (SupplierHib) session.get(SupplierHib.class, Long.valueOf(id));
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    public List<Supplier> getAll() {
        return session.getNamedQuery(SUPPLIER_GET_ALL_QUERY).list();
    }

    /**
     * {@inheritDoc}
     */
    public void addItem(long supplierId, long itemId) {
        SupplierHib supplier = get(supplierId);
        addItem(supplier, itemId);
    }

    /**
     * {@inheritDoc}
     */
    public void addItems(long supplierId, List<Long> itemIds) {
        SupplierHib supplier = get(supplierId);
        for (Long itemId : itemIds) {
            addItem(supplier, itemId);
        }
    }

    /**
     * {@inheritDoc}
     */
    public void removeItem(long supplierId, long itemId) {
        SupplierHib supplier = get(supplierId);
        removeItem(supplier, itemId);
    }

    /**
     * {@inheritDoc}
     */
    public void removeItems(long supplierId, List<Long> itemIds) {
        SupplierHib supplier = get(supplierId);
        for (Long itemId : itemIds) {
            removeItem(supplier, itemId);
        }
    }

    /**
     * {@inheritDoc}
     */
    public void update(long supplierId, String name, String email, String address, String phoneNumber) {
        SupplierHib supplier = get(supplierId);
        supplier.setName(name);
        supplier.setEmail(email);
        supplier.setAddress(address);
        supplier.setPhoneNumber(phoneNumber);
    }

    /**
     * {@inheritDoc}
     */
    public void updateAddress(long supplierId, String address) {
        SupplierHib supplier = get(supplierId);
        supplier.setAddress(address);
    }

    /**
     * {@inheritDoc}
     */
    public void updateEmail(long supplierId, String email) {
        SupplierHib supplier = get(supplierId);
        supplier.setEmail(email);
    }

    /**
     * {@inheritDoc}
     */
    public void updateName(long supplierId, String name) {
        SupplierHib supplier = get(supplierId);
        supplier.setName(name);
    }

    /**
     * {@inheritDoc}
     */
    public void updatePhoneNumber(long supplierId, String phoneNumber) {
        SupplierHib supplier = get(supplierId);
        supplier.setPhoneNumber(phoneNumber);
    }

    private ItemHib getItem(long itemId) {
        return (ItemHib) session.get(ItemHib.class, Long.valueOf(itemId));
    }

    private void addItem(SupplierHib supplier, long itemId) {
        ItemHib item = getItem(itemId);
        supplier.addItem(item);
    }

    private void removeItem(SupplierHib supplier, long itemId) {
        ItemHib item = getItem(itemId);
        supplier.getItems().remove(item);
    }

}
